package com.heima.datasync.service.impl;

import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.service.EsDataService;
import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.vos.SearchArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mrchen
 * @date 2022/1/12 9:45
 */
@Service
public class EsDataServiceImpl implements EsDataService {
    public static final String APP_INFO_ARTICLE = "app_info_article";
    @Autowired
    ApArticleMapper apArticleMapper;
    @Autowired
    EsService<SearchArticleVO> esService;
    @Override
    public ResponseResult dataInit() {
        //1 . 查询指定索引库是否存在
        boolean isHas = esService.existIndex(APP_INFO_ARTICLE);
        //2. 如果存在 删除旧的索引库
        if (isHas) {
            esService.deleteIndex(APP_INFO_ARTICLE);
        }
        //3. 创建新的文章索引库
        esService.createIndex("{\n" +
                "    \"mappings\":{\n" +
                "        \"properties\":{\n" +
                "            \"id\":{\n" +
                "                \"type\":\"long\"\n" +
                "            },\n" +
                "            \"publishTime\":{\n" +
                "                \"type\":\"date\"\n" +
                "            },\n" +
                "            \"layout\":{\n" +
                "                \"type\":\"integer\"\n" +
                "            },\n" +
                "            \"images\":{\n" +
                "                \"type\":\"keyword\",\n" +
                "                \"index\": false\n" +
                "            },\n" +
                "           \"staticUrl\":{\n" +
                "                \"type\":\"keyword\",\n" +
                "                \"index\": false\n" +
                "            },\n" +
                "            \"authorId\": {\n" +
                "          \t\t\"type\": \"long\"\n" +
                "       \t\t},\n" +
                "          \"title\":{\n" +
                "            \"type\":\"text\",\n" +
                "            \"analyzer\":\"ik_smart\"\n" +
                "          }\n" +
                "        }\n" +
                "    }\n" +
                "}",APP_INFO_ARTICLE);
        //4.  去数据库查询所有的文章数据
        List<ApArticle> allArticles = apArticleMapper.findAllArticles();
        //    apArticle  ==>   SearchArticleVO
        List<SearchArticleVO> searchArticleVOList = allArticles.stream().map(apArticle -> {
            SearchArticleVO searchArticleVO = new SearchArticleVO();
            BeanUtils.copyProperties(apArticle, searchArticleVO);
            return searchArticleVO;
        }).collect(Collectors.toList());
        //5.  使用es的批量导入功能 导入文章数据到索引库
        esService.saveBatch(searchArticleVOList,APP_INFO_ARTICLE);
        return ResponseResult.okResult();
    }
}